package com.ding.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author ding
 * @date 2021/7/17 19:44
 * 8锁，就是关于锁的8个问题
 * 5. 静态的同步方法
 * 6. 两个对象
 */


public class Test3 {
    public static void main(String[] args) {
        Phone3 phone1 = new Phone3();
        Phone3 phone2 = new Phone3();
        new Thread(() -> {
            phone1.sendSms();
        }, "A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() -> {
            phone2.call();
        }, "B").start();
    }


}

class Phone3 {

    //    synchronized 锁的对象是方法的调用者！
//    static静态方法
//    类一加载就有了，class模板，锁的是class
    public static synchronized void sendSms() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public static synchronized void call() {


        System.out.println("打电话");
    }
}